package com.ruoyi.member.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.member.domain.Member;
import com.ruoyi.member.service.IMemberService;

/**
 * 会员管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController {
    @Autowired
    private IMemberService memberService;

    /**
     * 获取会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 根据会员编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId) {
        return success(memberService.selectMemberById(memberId));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Member member) {
        if (!memberService.checkUsernameUnique(member.getUsername())) {
            return error("新增会员'" + member.getUsername() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(member.getMobile()) && !memberService.checkMobileUnique(member)) {
            return error("新增会员'" + member.getUsername() + "'失败，手机号码已存在");
        }
        member.setCreateBy(getUsername());
        return toAjax(memberService.registerMember(member));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Member member) {
        if (StringUtils.isNotEmpty(member.getMobile()) && !memberService.checkMobileUnique(member)) {
            return error("修改会员'" + member.getUsername() + "'失败，手机号码已存在");
        }
        member.setUpdateBy(getUsername());
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds) {
        return toAjax(memberService.deleteMemberByIds(memberIds));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('member:member:resetPwd')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody Member member) {
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        member.setUpdateBy(getUsername());
        return toAjax(memberService.resetMemberPwd(member));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Member member) {
        member.setUpdateBy(getUsername());
        return toAjax(memberService.updateMemberStatus(member));
    }

    /**
     * 修改积分
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changePoints")
    public AjaxResult changePoints(@RequestBody Member member) {
        member.setUpdateBy(getUsername());
        return toAjax(memberService.updateMemberPoints(member));
    }
} 